package com.hwua.demo02;

import java.io.*;


/**
 * @author Administrator
 */
public class Demo01 {
    public static void main(String[] args) {
        Demo01 d = new Demo01();
        File file = new File("E:\\A\\A.txt");
        try {
            System.out.println( d.readFile(file));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public String readFile(File file) throws FileNotFoundException, IOException {
        if(!file.exists()){
            throw new FileNotFoundException("读取文件失败");
        }
        //--根据文件构建字节输入流对象
        InputStream is = new FileInputStream(file);

        byte[] buffer = new byte[1024];
        //--提供一个可变长度的字符串，用于接收每次读取的内容
        StringBuilder sBuilder = new StringBuilder();
        //--保证每次读取的字节数
        int length = 0;
        //--循环读取内容，循环条件，当文件读取到末尾是返回-1
        while(-1 != (length = is.read(buffer))){
            String str = new String(buffer,0,length,"GBK");
            sBuilder.append(str);
        }
        return sBuilder.toString();
    }
}
